package cn.zhang.violet.report

import cn.zhang.violet.config.ConfigHandler
import org.apache.spark.sql.{DataFrame, SQLContext, SaveMode}
import org.apache.spark.{SparkConf, SparkContext}

object AppReportSql {
  def main(args: Array[String]): Unit = {
    val sparkConf = new SparkConf()
    sparkConf.setAppName("地域分布")
    sparkConf.setMaster("local[*]")
    sparkConf.set("spark.serializer","org.apache.spark.serializer.KryoSerializer")

    val sc = new SparkContext(sparkConf)

    val sQLContext = new SQLContext(sc)
    //读取数据
    val dataFrame = sQLContext.read.parquet(ConfigHandler.parquetFilePath)
    //按照需求分析这张表的数据
    dataFrame.registerTempTable("adlog")

    //按省份来分类的各个数据

    sQLContext.sql(
      """
        |select provincename, cityname,
        |sum(if(requestmode=1 and processnode>=1, 1, 0)) adRawReq,
        |sum(if(requestmode=1 and processnode>=2, 1, 0)) adEffReq,
        |sum(case when requestmode=1 and processnode=3 then 1 else 0 end) adReq,
        |sum(case when iseffective=1 and isbilling=1 and isbid=1 and adorderid!=0 then 1 else 0 end) adRtbReq,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then 1 else 0 end) adSuccReq,
        |sum(case when requestmode=2 and iseffective=1 then 1 else 0 end) adShow,
        |sum(case when requestmode=3 and iseffective=1 then 1 else 0 end) adClick,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then winprice/1000 else 0 end) adCost,
        |sum(case when iseffective=1 and isbilling=1 and iswin=1 then adpayment/1000 else 0 end) adConsumption
        |
        |from adlog
        |group by provincename, cityname
      """.stripMargin).write.mode(SaveMode.Overwrite).jdbc(ConfigHandler.url,"violet_prodata_sqljoinnum1", ConfigHandler.props)




    sc.stop()
  }
}
